package com.mali.malibackendproduct.controller;

import com.mali.backendcommon.common.Result;
import com.mali.backendcommon.constant.MessageConstant;
import com.mali.backendcommon.util.AliOssUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

/**
 * @Author:吴键霏
 * @Date：2024/6/16 10:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/product")
@Slf4j
@CrossOrigin(origins = "*", allowedHeaders = "*")
public class ProductController {


    @Autowired
    private AliOssUtil aliOssUtil;



    @PostMapping("/image/upload")
    public Result upload(MultipartFile file)
    {

        try {
            String originalName=file.getOriginalFilename();
            String fileType=originalName.substring(originalName.lastIndexOf("."));
            String fileEndName= UUID.randomUUID().toString()+fileType;
            String filePath=aliOssUtil.upload(file.getBytes(), fileEndName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败，{}",e);
        }
        return Result.error("文件上传失败");

    }
}
